// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SitemapActionResult.cs" company="ecblog.codeplex.com">
//   emrecoskun.net - ibrahimatay.com
// </copyright>
// <summary>
//   Sitemap Action Result
// </summary>
// --------------------------------------------------------------------------------------------------------------------
 
namespace EcBlog.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Web.Mvc;
    using System.Xml;

    using EcBlog.Models;

    /// <summary>
    /// Sitemap Action Result
    /// </summary>
    public class SitemapActionResult : ActionResult
    {
        private readonly string siteUrl;
        private readonly List<Page> pages;
        private readonly string urlSlugFormat;
        private readonly string urlSuffix;

        /// <summary>
        /// Initializes a new instance of the <see cref="SitemapActionResult"/> class.
        /// </summary>
        /// <param name="siteUrl">The site URL.</param>
        /// <param name="pages">The pages.</param>
        /// <param name="urlSlugFormat">The URL slug format.</param>
        /// <param name="urlSuffix">The URL suffix.</param>
        public SitemapActionResult(string siteUrl, List<Page> pages, string urlSlugFormat, string urlSuffix)
        {
            this.siteUrl = siteUrl;
            this.pages = pages;
            this.urlSlugFormat = urlSlugFormat;
            this.urlSuffix = urlSuffix;
        }

        /// <summary>
        /// Enables processing of the result of an action method by a custom type that inherits from the <see cref="T:System.Web.Mvc.ActionResult"/> class.
        /// </summary>
        /// <param name="context">The context in which the result is executed. The context information includes the controller, HTTP content, request context, and route data.</param>
        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.ContentType = "text/xsl";
            using (var writer = XmlWriter.Create(context.HttpContext.Response.Output))
            {
                writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
                writer.WriteStartElement("url");
                writer.WriteElementString("loc", this.siteUrl);
                writer.WriteElementString("lastmod", DateTime.Now.ToString("yyyy-MM-dd"));
                writer.WriteElementString("changefreq", "daily");
                writer.WriteElementString("priority", "1.0");
                writer.WriteEndElement();

                foreach (var page in this.pages)
                {
                    var url =
                        this.urlSlugFormat
                            .ToLower()
                            .Replace("{siteurl}", this.siteUrl)
                            .Replace("{year}", page.Published.Year.ToString(CultureInfo.InvariantCulture))
                            .Replace("{month}", page.Published.Month.ToString(CultureInfo.InvariantCulture))
                            .Replace("{day}", page.Published.Day.ToString(CultureInfo.InvariantCulture))
                            .Replace("{slug}", page.Slug) + this.urlSuffix;
                    writer.WriteStartElement("url");
                    writer.WriteElementString("loc", url);
                    writer.WriteElementString("lastmod", page.LastModified.ToString("yyyy-MM-dd"));
                    writer.WriteElementString("changefreq", "daily");
                    writer.WriteElementString("priority", "0.5");
                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
                writer.Flush();
                writer.Close();
            }
        }
    }
}